﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MikuMikuLibrary;
using MikuMikuLibrary.Archives;
using MikuMikuLibrary.Archives.Farc;

namespace ft_module_parser.Shader
{
    class Data
    {
        public List<StrReplace> str = new List<StrReplace>();
        public List<Data> data = new List<Data>();
        public FarcArchive farc;
        public void ApplyStrReplace()
        {
            foreach (var i in str)
            {
                System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex(i.filename);
                var farcentries = farc.Entries.Where(c => regEx.IsMatch(c));
                Console.WriteLine(farcentries.Count());
                foreach (var c in farcentries)
                {
                    var thefile = farc.Open(c, MikuMikuLibrary.Archives.EntryStreamMode.MemoryStream);
                    var streamreader = new StreamReader(thefile);
                    string strmessage;
                    strmessage = streamreader.ReadToEnd();
                    strmessage = strmessage.Replace(i.before, i.after);
                    byte[] byteArray = Encoding.Default.GetBytes(strmessage);
                    MemoryStream stream = new MemoryStream(byteArray);
                    farc.Add(c, stream, true, ConflictPolicy.Replace);
                }
            }
        }

        public void ApplyData()
        {

        }
    }
}
